package org.csu.onlineedubackend.mapper;

import org.apache.ibatis.annotations.*;
import org.csu.onlineedubackend.entity.ChatGroup;

import java.util.List;

@Mapper
public interface ChatGroupMapper {
    
    @Insert("INSERT INTO chat_groups (conversation_id, name, owner_id, created_at) VALUES (#{conversationId}, #{name}, #{ownerId}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ChatGroup chatGroup);
    
    @Select("SELECT * FROM chat_groups WHERE id = #{id}")
    ChatGroup findById(Long id);
    
    @Select("SELECT * FROM chat_groups WHERE conversation_id = #{conversationId}")
    ChatGroup findByConversationId(Long conversationId);
    
    @Select("SELECT g.* FROM chat_groups g " +
            "JOIN conversation_participants cp ON g.conversation_id = cp.conversation_id " +
            "WHERE cp.user_id = #{userId}")
    List<ChatGroup> findByUserId(Integer userId);
    
    @Update("UPDATE chat_groups SET name = #{name} WHERE id = #{id}")
    int updateName(@Param("id") Long id, @Param("name") String name);

    @Select("SELECT conversation_id FROM chat_groups WHERE id = #{groupId}")
    Long findConversationIdByGroupId(@Param("groupId") Long groupId);

    @Delete("DELETE FROM chat_groups WHERE id = #{id}")
    int deleteById(Long id);
} 